let Controller = {
	mounted() {
		this.initData();
	},
	activated() {
		this.initData();
		this.requestDetail();
	},
	methods: {
		initData() {
			const param = JSON.parse(this.$route.query.data);
			this.Model.queryData = _.cloneDeep(param);
		},
		onApproval() {
			const orderId = this.Model.queryData.id;
			const params = [orderId];
			if (params && params.length > 0) {
				this.$confirm('是否确认审核?', '提示', {
					confirmButtonText: '确定',
					cancelButtonText: '取消',
				}).then(() => {
					this.$ajax
						.post(this.apiTest + `/sale/onSale/order/afterSale/approval`, params)
						.then(response => {
							const data = response.data;
							if (data.status == 200 && data.result == 1) {
								this.$message({
									type: 'success',
									message: '已成功审核'
								});
								this.requestDetail();
							} else {
								this.$message({
									type: 'error',
									message: data.msg
								});
							}
						})
						.catch(error => {
							this.$message.error('请求错误');
						});
				}).catch(() => {

				});
			}
		},
		onAddAfterSoleCost() {
			//chooseType 1单选，2文本框
			//choose	单选时候的值
			//lostCount 文本框默认值
			/* 
			不需更改就回传
			 accessoriesType: "1"
			 id: 1
			 isDel: false
			 ruleType: "01"
			 */

			this.$ajax
				.get(this.apiTest + `/sale/onSale/afterOrder/refund/showCost/${this.Model.goodInfoId}`)
				.then(response => {
					const data = response.data;
					if (data.status == 200) {
						this.Model.editDialogItemOption = data.result.filter(item => {
							return item = item;
						});
						this.Model.addCostDialogFlag = true;
					} else {
						this.$message({
							type: 'error',
							message: data.msg
						});
					}
				})
				.catch(error => {});
		},
		onSaveAfterSoleCost() {
			const param = {
				currency: 'RMB',
				orderGoodId: this.Model.goodsId,
				list: this.Model.editDialogItemOption
			};
			this.$ajax.post(this.apiTest + `/sale/onSale/afterOrder/refund/addCost`, param)
				.then(res => {
					const data = res.data;
					if (data.status == 200) {
						this.$message({
							type: 'success',
							message: '添加成功！'
						});
						this.Model.addCostDialogFlag=false;
						thie.requestDetail();
					} else {
						this.$message.error(data.msg);
					}
				}).catch(err => {})
		},
		requestShowCost() {
			const goodId = this.Model.queryData.id;
		},
		deleteOrderGifts(id) {
			this.$confirm('是否确认删除赠品?', '提示', {
				confirmButtonText: '确定',
				cancelButtonText: '取消',
			}).then(() => {
				this.$ajax.delete(this.apiTest + `/sale/saleOrderGifts/${id}`, {})
					.then(response => {
						const data = response.data;
						if (data.status == 200) {
							this.$message.success('删除成功');
							this.requestDetail();
						}
					}).catch(err => {

					})
			})

		},
		requestDetail() {
			const orderId = this.Model.queryData.id;
			if (orderId) this.$ajax
				.get(this.apiTest + `/sale/onSale/order/getRefundDetail/${orderId}`)
				.then(response => {
					const data = response.data;
					const result = data.result;
					this.Model.idCard = result.idCard;
					// this.Model.goodsId = result.id;  危宇说注释
					this.Model.returnGoodsInfoTableCol = _.cloneDeep(this.Model.defaultReturnGoodsInfoTableCol);
					const feesList = result.returnGoodsInfo.feesList.map((item,index)=>{
						return {prop:`desc${item.id}`, label:item.desc, width:"120",}
					});
					for(let i=0;i<feesList.length;i++){
						this.Model.returnGoodsInfoTableCol.splice(this.Model.returnGoodsInfoTableCol.length-1,0,feesList[i]);
					}
					this.Model.saleOrderGiftsList = result.saleOrderGiftsList;
					this.Model.saleOrderPayList = result.saleOrderPayList;
					this.Model.refundList = result.refundList;
					this.Model.saleLogList = result.saleLogList;
					for(let i in result.returnGoodsInfo.feesList){
						const item = result.returnGoodsInfo.feesList[i];
						result.returnGoodsInfo['desc'+item.id] = item.goodsCostFee;
					}
					this.Model.tableData.returnGoodsInfo = result.returnGoodsInfo ? [result.returnGoodsInfo] : [];
					this.Model.goodInfoId = result.returnGoodsInfo.goodInfoId;
					this.Model.goodsId = result.returnGoodsInfo.goodsId;
					this.Model.tableData.list = _.cloneDeep(result.list);
					this.Model.aftersaleStatus = result.aftersaleStatus;
					this.setStepData(result.saleOrderStatusList, result.aftersaleStatus);
					this.Model.tabFormInline = [{
							label: '售中单号',
							value: this.Model.queryData.saleNumber,
						},
						{
							label: '售后状态',
							value: data.result.aftersaleStatusName
						},
						{
							label: 'sap售中单号',
							value: this.Model.queryData.sapSaleNumber
						},
						{
							label: '原销售定单号',
							value: this.Model.queryData.originNumber
						},
						{
							label: '原sap销售订单号',
							value: this.Model.queryData.originSapNumber
						}
					];
					this.Model.tableFormInline = [{
							label: '订单编号',
							value: data.result.orderNumber || '',
						},
						{
							label: '原sap销售订单号',
							value: data.result.sapOrderNumber || '',
						},
						{
							label: '下单人',
							value: data.result.orderBuyer || '',
						},
						{
							label: '配送类型',
							value: data.result.deliveryType || '',
						},
						{
							label: '发货平台',
							value: data.result.shipperStore || '',
						},
						{
							label: '销售来源',
							value: data.result.saleOrigin || '',
						},
						{
							label: '销售员',
							value: data.result.saleMan || '',
						}
					];
				})
				.catch(error => {
					this.$message.error('请求错误');
				});
		},
		setStepData(list, status) {
			let activeStep = 0,
				step = [];
			for (let i = 0; i < list.length; i++) {
				if (list[i].code == status) activeStep = i;
				step.push({
					title: list[i].codeName,
					description: list[i].createTime
				})
			};
			this.Model.refundStep = {
				activeStep,
				step
			}
		},
		tableDetail(row) {
			this.Model.detailDialogFlag = true;
		},
		warehousing(row) {
			this.Model.warehousingDialogStatus = row.status;
			this.Model.warehousingDialogFlag = true;
		},
		handleSelectionChange(val) {},
		handleCurrentChange(val) {
			this.Model.pager.params.pageIndex = val;
		},
		handleSizeChange(val) {
			this.Model.pager.params.pageSize = val;
		},
		cardClick(color) {
			this.Model.cardSelected = color;
		},
		showChild(val) {
			this.Model.searchFromData = val;
		},
		toSearch(val) {},
		toClear(val) {
			this.Model.searchFromData = this.Model.defaultSearchFromData;
		},
		toExport(val) {},
		onTabChange(val) {
			switch (val) {
				case 1:
					break;
				case 2:
					break;
				case 3:
					break;
			}
		},
		toggleTableDetail(val) {
			const obj = this.Model[val];
			if (obj.flag === false) {
				this.Model[val].src = require('@/assets/img/icon_collapse.png');
				this.Model[val].flag = true;
			} else {
				this.Model[val].src = require('@/assets/img/icon_unfold.png');
				this.Model[val].flag = false;
			}
		},
		onDialogEdit(flag, val) {
			this.Model.editDialogFlag = flag;
			if (flag === false) return;
			const obj = this.Model[val];
			this.Model.editDialogFormData = _.cloneDeep(this.Model.defaultEditDialogFormData);
			this.getAddress();
		},
		changeAddress(val) {
			if (!val) {
				this.getAddress();
			}
		},
		getAddress() {
			const idCard = this.Model.idCard;
			this.$ajax.post(this.apiTest + `/sale/saleOrder/getAddressByIdCard/${idCard}`, {})
				.then(response => {
					const data = response.data;
					if (data.status == 200) {
						this.Model.addressList = data.result;
					}
				}).catch(err => {})
		},
		updateBaseSaleOrder() {
			const orderId = this.Model.queryData.id;
			const data = {
				...this.Model.editDialogFormData,
				orderId,
				idCard: this.Model.idCard,
			};
			let params;
			if (data.shippingMethod === '1') {
				params = {
					orderId,
					idCard: this.Model.idCard,
					shippingMethod: data.shippingMethod,
					shipper: data.shipper,
					originChannel: data.originChannel,
					salesman: data.salesman,
				}
			} else {
				if (this.Model.editDialogNewAddress == false) {
					const index = this.Model.editDialogItemAddressBoxRadio;
					const list = this.Model.addressList;
					data.recipientName = list[index].name;
					data.recipientPhone = list[index].phone;
					data.detailedAddress = list[index].address;
				}
				params = data;
			}
			this.$ajax.post(this.apiTest + `/sale/saleOrder/updateBaseSaleOrder`, params)
				.then(response => {
					const data = response.data;
					if (data.status == 200) {
						this.requestDetail();
						this.onDialogEdit(false);
						this.$message({
							type: 'success',
							message: '已成功保存'
						});

					} else {
						this.$message.error(data.msg);
					}
				}).catch(err => {
					this.$message.error(err);
					this.onDialogEdit(false);
				});
		},
		getStatusText(type, value) {
			let text = '';
			switch (type) {
				case 'aftersaleStatus':
					/* 售后：0退款中/1退款成功/2退款拒绝/ 售中：3待审核/4已取消/5变更成功/6待用户支付/7待平台退款 */
					switch (value) {
						case 0:
							text = '退款中';
							break;
						case 1:
							text = '退款成功';
							break;
						case 2:
							text = '退款拒绝';
							break;
						case 3:
							text = '待审核';
							break;
						case 4:
							text = '已取消';
							break;
						case 5:
							text = '变更成功';
							break;
						case 6:
							text = '待用户支付';
							break;
						case 7:
							text = '待平台退款';
							break;
						default:
							text = '暂无';
							break;
					}
					break;
				default:
					break;
			}
			return text;
		},
	}
}
export default Controller;
